package com.xinan.dmgl.cggl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.core.BaseController;
import com.jfinal.core.Const;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.SearchSql;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.JsonRender;
import com.xinan.utils.Constants;
import com.xinan.utils.Pinyin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CgglWpController extends BaseController {

	public void index() {
		render("/WEB-INF/views/cggl/wpgl/index.html");
	}

	public void add() {
		String lbid = getPara("id");
		String name = getPara("name");
		String pid = getPara("pid");
		setAttr("lbid", lbid);
		setAttr("name", name);
		setAttr("pid", pid);
		render("/WEB-INF/views/cggl/wpgl/add.html");
	}

	public void gl() {
		String cjid = getPara("cjid");
		setAttr("cjid", cjid);
		render("/WEB-INF/views/cggl/wpgl/glwp.html");
	}

	public void glcj() {
		String cjid = getPara("cjid");
		String wpid = getPara("id");
		String[] wp = wpid.split(",");
		for (int i = 0; i < wp.length; i++) {
			Record xx = new Record();
			xx.set("wpid", wp[i]).set("cjid", cjid);
			Db.save("wp_cj", xx);
		}
	//	renderJson("商品关联成功");
		render(new JsonRender("商品关联成功").forIE());

	}

	// 根据传入参数获取拼音头下拉框组件数据
	public void getWpxx() {
		String sid = "select  a.yxbz,a.sptcfs, a.sptc,a.id,a.wpmc as text,a.ckdj,a.kcsl,a.sptc,a.wpbh,a.archivesfileid," +
				"(select sum(b.wpsl)  from cpgl_rk b where b.wpid=a.id  ) as rksl " +
				",(select sum(b.cksl)  from cpgl_ck b where b.wpid=a.id  ) as cksl    from cggl_wp a";
		String term = getPara("term");
		String wherers = "";
		if (StringKit.notBlank(term.trim())) {
			wherers = " and ( wpbh like '%" + term + "%' or wpmc like '%"
					+ term + "%' or wppy like '%" + term + "%') order by wpmc";
		}
		String where = " where  yxbz !='0' " + wherers;
		int pageNumber = getParaToInt("page");
		int pageSize = getParaToInt("page_limit");
		List<Record> newlist = Db.find(sid + where + " limit " + pageNumber
				+ "," + pageSize);
		renderJson(newlist);

		// Page<JsKhgl> page = JsKhgl.dao.page(pageNumber, pageSize, where);
		// System.out.println("++++++++++++++++++"+page.getRows());
		// renderJson(page);
	}

	// 根据传入参数获取拼音头下拉框组件数据
	public void getCj() {
		String sid = "select  id,cjmc as text,lxr,lxdh,wx  from dm_cjgl";
		String term = getPara("term");
		String wherers = "";
		if (StringKit.notBlank(term.trim())) {
			wherers = " and (  cjmc like '%" + term + "%' or cjdm like '%"
					+ term + "%') order by cjmc";
		}
		String where = " where  1=1 " + wherers;
		int pageNumber = getParaToInt("page");
		int pageSize = getParaToInt("page_limit");
		List<Record> newlist = Db.find(sid + where + " limit " + pageNumber
				+ "," + pageSize);
		renderJson(newlist);

		// Page<JsKhgl> page = JsKhgl.dao.page(pageNumber, pageSize, where);
		// System.out.println("++++++++++++++++++"+page.getRows());
		// renderJson(page);
	}

    // 根据传入参数获取拼音头下拉框组件数据
    public void getFwzl() {
        String sid = "select  *,fwzl as text  from hygl_fwgl where yxbz !='0' ";
        String term = getPara("term");

        int pageNumber = getParaToInt("page");
        int pageSize = getParaToInt("page_limit");
        List<Record> newlist = Db.find(sid  + " limit " + pageNumber
                + "," + pageSize);
        renderJson(newlist);

        // Page<JsKhgl> page = JsKhgl.dao.page(pageNumber, pageSize, where);
        // System.out.println("++++++++++++++++++"+page.getRows());
        // renderJson(page);
    }


    public void addlb() {
		String lbid = getPara("lbid");
		String name = getPara("name");
		String pid = getPara("pid");
		setAttr("lbid", lbid);
		setAttr("name", name);
		setAttr("pid", pid);
		String id = getPara("id");
		String where = "";
		List<Record> list = null;
		if (StringKit.notBlank(id)) {
			where += " where id =" + id + "  ";
			list = CgglWplb.dao.findtree(where);
		} else {
			list = CgglWplb.dao.findtree("where id= null");
		}
		setAttr("list", list);
		render("/WEB-INF/views/cggl/wpgl/add_lb.html");
	}

	public void addwp() {
		String lbid = getPara("lbid");
		String name = getPara("name");
		String pid = getPara("pid");
		String id = getPara("id");
		List<Record> pic = null;
		List<Record> newlist = Db
				.find("select  * from cggl_wp     where    id=  '" + id + "'");
		if (newlist.size() > 0) {
			String archivesfileid = newlist.get(0).get("archivesfileid");
			pic = Db.find("select  * from oa_archivesfile     where    archivesfileid=  '"
					+ archivesfileid + "'");

		}
		setAttr("lbid", lbid);
		setAttr("name", name);
		setAttr("pid", pid);
		setAttr("list", newlist);
		setAttr("pic", pic);

		render("/WEB-INF/views/cggl/wpgl/add_wp.html");
	}

	/**
	 * 鏂板椤甸潰
	 */
	public void newModes() {
		String id = getPara("id");
		List<Record> newlist = Db.find("select  * from     where    id=  '"
				+ id + "'");
		setAttr("newlist", newlist);
		render("test/cgglWp.html");
	}

	public void getWpTree() {
		String id = getPara("pid");
		String yxbz = getPara("yxbz");
		String where = "";
		if (StringKit.notBlank(yxbz)) {
			where += " where yxbz =" + yxbz + "  ";
		}
		List<Record> newlist = CgglWplb.dao.findtree(where);
		JSONObject jo1 = new JSONObject();
		// / 增加根节点
		jo1.put("id", "0");
		jo1.put("pId", "0");
		jo1.put("pid", "0");

		jo1.put("name", "无");
		JSONArray jsonArr = new JSONArray();
		if (newlist.size() == 0) {
			jsonArr.add(jo1);
			renderJson(jsonArr);
		} else {
			for (int j = 0; j < newlist.size(); j++) {
				JSONObject jo = new JSONObject();
				jo.put("id", newlist.get(j).get("id"));
				jo.put("pId", newlist.get(j).get("pId"));
				jo1.put("pid", "0");

				jo.put("name", newlist.get(j).get("name"));
				jsonArr.add(jo);
			}
			jsonArr.add(jo1);
		}
		renderJson(jsonArr);

	}

	/**
	 * 在增加了SearchSql拦截之后，就可以根据页面的配置信息
	 * 自动装配查询语句，并放入request中，可以通过getAttr(Const.SEARCH_SQL)函数获取到 根据实际需要自动组装到sql语句中
	 */
	@Before(SearchSql.class)
	public void query() {
		int pageNumber = getParaToInt("page");
		int pageSize = getParaToInt("rows");
		String where = getAttr(Const.SEARCH_SQL);
		String search = getPara("search_EQ_lbid");
		System.out.println(search);

		if (StringKit.notBlank(search)) {
			if (search.equals("0")) {
				where = "";
			}
		}
		Page<CgglWp> page = CgglWp.dao.page(pageNumber, pageSize, where);
		Map root = new HashMap();
		// / jqgrid用 分页显示
		root.put("total", page.getTotalPage());
		root.put("page", page.getPageNumber());
		root.put("records", page.getTotalRow());
		root.put("rows", page.getList());
		renderJson(JsonKit.toJson(root));
	}

	/**
	 * 类别保存
	 */
	public void savelb() {
		String lbmc = getPara("lbmc");
		List<Record> max = Db
				.find("Select count(*) as max  from cggl_wplb where  lbmc='"
						+ lbmc + "'");
		String maxs = String.valueOf(max.get(0).get("max"));
		if (!maxs.equals("0")) {
		//	renderJson("存在此类别！");
			render(new JsonRender("存在此类别！").forIE());

		} else {
			if (getModel(CgglWplb.class).save()) {
			//	renderJson(Constants.SAVESUCCESS);
				render(new JsonRender(Constants.SAVESUCCESS).forIE());

			} else {
		//		renderJson(Constants.SAVEFAILED);
				render(new JsonRender(Constants.SAVEFAILED).forIE());

			}
		}

	}

	/**
	 * 物品保存
	 */
	public void savewp() {
		String wpmc = getPara("wpmc");
		String wpbh = getPara("wpbh");
		String wppy = Pinyin.converterToFirstPinyin(wpmc);
		// 类别名称物品名称不重复
		List<Record> max = Db
				.find("Select count(*) as max  from cggl_wp where  yxbz='1' and  wpmc='"
						+ wpmc + "' or wpbh= '" + wpbh + "'");
		String maxs = String.valueOf(max.get(0).get("max"));
		if (!maxs.equals("0")) {
			//renderJson("存在此物品！");
			render(new JsonRender("存在此物品！").forIE());

		} else {
			if (getModel(CgglWp.class).set("wppy", wppy).save()) {
			//	renderJson(Constants.SAVESUCCESS);
				render(new JsonRender(Constants.SAVESUCCESS).forIE());

			} else {
		//		renderJson(Constants.SAVEFAILED);
				render(new JsonRender(Constants.SAVEFAILED).forIE());

			}
		}

	}

	/**
	 * 类别修改
	 */
	public void updatelb() {
		String lbmc = getPara("lbmc");
		String id = getPara("id");

		List<Record> max = Db
				.find("Select count(*) as max  from cggl_wplb where lbmc='"
						+ lbmc + "'  and id != '" + id + "'");
		String maxs = String.valueOf(max.get(0).get("max"));
		if (!maxs.equals("0")) {
		//	renderJson("存在此类别！");
			render(new JsonRender("存在此类别！").forIE());

		} else {
			if (getModel(CgglWplb.class).update()) {
			//	renderJson(Constants.EDITSUCCESS);
				render(new JsonRender(Constants.EDITSUCCESS).forIE());

			} else {
			//	renderJson(Constants.EDITFAILED);
				render(new JsonRender(Constants.EDITFAILED).forIE());

			}
		}

	}

	/**
	 * 物品修改
	 */
	public void updatewp() {
		String wpmc = getPara("wpmc");
		String wpbh = getPara("wpbh");
		String id = getPara("id");

		// 类别名称物品名称不重复
		List<Record> max = Db
				.find("Select count(*) as max  from cggl_wp where wpmc='"
						+ wpmc + "' and id!='" + id + "'");
		String maxs = String.valueOf(max.get(0).get("max"));
		if (!maxs.equals("0")) {
		//	renderJson("存在此物品！");
			render(new JsonRender("存在此物品！").forIE());

		} else {
			if (getModel(CgglWp.class).update()) {
			//	renderJson(Constants.EDITSUCCESS);
				render(new JsonRender(Constants.EDITSUCCESS).forIE());

			} else {
			//	renderJson(Constants.EDITFAILED);
				render(new JsonRender(Constants.EDITFAILED).forIE());

			}
		}
	}

	/**
	 * Post璇锋眰鐨勮彍鍗曞垹闄ゅ嚱鏁帮紝鍙互鎺ュ彈鑿滃崟鐨刬d锛屽苟鏍规嵁鑿滃崟杩涜鐗╃悊鍒犻櫎锛屽苟杩斿洖褰卞搷琛屾暟锛屾嵁姝ゅ垽鏂槸鍚︽
	 * 搷浣滄垚鍔�
	 */
	public void delete() {
		if (CgglWplb.dao.deleteById(getParaToInt())) {
		//	renderJson(Constants.DELETESUCCESS);
			render(new JsonRender(Constants.DELETESUCCESS).forIE());

		} else {
		//	renderJson(Constants.DELETEFAILED);
			render(new JsonRender(Constants.DELETEFAILED).forIE());

		}
	}

	public void deletewp() {
		if (getModel(CgglWp.class).set("yxbz", "0").update()) {
		//	renderJson(Constants.DELETESUCCESS);
			render(new JsonRender(Constants.DELETESUCCESS).forIE());

		} else {
	//		renderJson(Constants.DELETEFAILED);
			render(new JsonRender(Constants.DELETEFAILED).forIE());

		}
	}

	public void getwp() {
		String mc = getPara("mc");
		String dm = getPara("dm");
		String dw = getPara("dw");
		if (StringKit.notBlank(mc)) {
			setAttr("mc", mc);
		}
		if (StringKit.notBlank(dm)) {
			setAttr("dm", dm);
		}
		if (StringKit.notBlank(dw)) {
			setAttr("dw", dw);
		}
		render("cggl/wpgl/wp.html");
	}

	public void getlist() {
		String mc = getPara("mc");
		String dm = getPara("dm");
		String dw = getPara("dw");
		String lbid = getPara("id");
		String name = getPara("name");
		String pid = getPara("pid");
		setAttr("lbid", lbid);
		setAttr("name", name);
		setAttr("pid", pid);
		if (StringKit.notBlank(mc)) {
			setAttr("mc", mc);
		}
		if (StringKit.notBlank(dm)) {
			setAttr("dm", dm);
		}
		if (StringKit.notBlank(dw)) {
			setAttr("dw", dw);
		}
		render("cggl/wpgl/wplist.html");

	}
}
